<template>
  <div class="comment-container">
    <el-card class="filter-card">
      <div slot="header" class="clearfix">
        <!--  面包屑路径导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>评论管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 数据表格 -->
      <el-table :data="comments" style="width: 100%" class="table-list">
        <el-table-column prop="title" label="标题" width="380">
        </el-table-column>
        <el-table-column prop="total_comment_count" label="总评论数" >
        </el-table-column>
        <el-table-column prop="fans_comment_count" label="粉丝评论数" > </el-table-column>
        <el-table-column prop="comment_status" label="评论状态" >
          <template slot-scope="scope">
            {{ scope.row.comment_status ? '正常' : '关闭'}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
        <template slot-scope="scope">
         <el-switch
            v-model="scope.row.comment_status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :disabled="scope.row.disable"
            @change="onStatusChange(scope.row)">
          </el-switch>
      </template>
    </el-table-column>
      </el-table>
    </el-card>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[10, 20, 50, 100]"
      :page-size.sync="pageSize"
      :current-page.sync="page"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalCount"
      background>
    </el-pagination>
  </div>
</template>
<script>
import { getComments, updateCommentStatus } from '@/api/comment'
export default {
  name: 'commentIndex',
  data() {
    return {
      tableData: [
        {
          title: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄',
          status: '正常'
        },
        {
          title: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄',
          status: '正常'
        },
        {
          title: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄',
          status: '正常'
        },
        {
          title: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄',
          status: '正常'
        }
      ],
      comments: [], // 评论列表
      page: 1, // 分页页码
      pageSize: 20, // 每页的大小
      totalCount: 0, // 总数据条数
      disable: false // 禁用， 默认不禁用
    }
  },
  methods: {
    handleDelete(index, row) {
      console.log(index, row)
    },
    // 每页条数
    handleSizeChange(page) {
      this.loadComments(1)
    },
    // 获取分页更新 【当前页】
    handleCurrentChange(page) {
      console.log(`当前页: ${page}`)
      // 页码改变，加载指定页码数据
      this.loadComments(page)
    },

    // 获取评论管理列表
    loadComments(page = 1) {
      // 让分页组件激活的页码和请求数据的页码保持一致
      this.page = page
      getComments({
        response_type: 'comment',
        page,
        per_page: this.pageSize
      }).then(res => {
        console.log(res)
        res.data.data.results.forEach(comments => {
          comments.disable = false
        })
        this.comments = res.data.data.results
        this.totalCount = res.data.data.total_count
      })
    },
    // 修改评论状态
    onStatusChange(comments) {
      comments.disable = true
      updateCommentStatus(
        comments.id.toString(),
        comments.comment_status
      ).then(res => {
        comments.disable = false
        this.$message({
          type: 'success',
          message: comments.comment_status ? '开启评论状态' : '关闭评论状态'
        })
      })
    }
  },
  created() {
    this.loadComments(1)
  },
  mounted() {},
  components: {},
  props: []
}
</script>

<style scoped lang = "less">
.table-list{
  margin-bottom:30px;
}
</style>
